<?php
require_once 'connect.php';

/*
 * @param $img  File of img
 * 
 * @return true or false
 * */
function uploadimg($img, $des) {
	$name = $img ['name'];
	$type = $img ['type'];
	$tmp_name = $img ['tmp_name'];
	
	$size = $img ['size'];
	$time = date ( "Y-m-d H:i:s" );
	$upload = "./images/" . time () . "_" . $name;
	move_uploaded_file ( $tmp_name, $upload );
	//if(move_uploaded_file($tmp_name,$upload)){		
	$query = "INSERT INTO imgfile(path,updatetime,imgdes,imgtitle,favor,normal,hate) values ('$upload','$time','$des','$name',0,0,0)";
	mysql_query ( $query );
	$id = mysql_insert_id ();

		//}
}

/*
 * Favor or hate a pic by a user
 * 1 presents favor
 * 2 presents normal
 * 3 presents hate
 * */
function addFavor($img_id, $user_id) {
	$query = "select * from imgfavor where iid=$img_id and uid='$user_id' and type=1";
	$result = mysql_query ( $query );
	if (mysql_affected_rows () == 0) {
		$query = "insert into imgfavor(iid,uid,type) values($img_id,'$user_id',1)";
		@mysql_query ( $query );
		$query = "update imgfile set favor=favor+1 where id=$img_id";
		@mysql_query ( $query );
	}
}

function getFavor($img_id) {
	$query = "select favor from imgfile where id=$img_id";
	$result = mysql_query ( $query );
	$favorarray = mysql_fetch_array ( $result );
	return $favorarray [0];
}

/*
 * Hate a img by a user
 * @param $img_id the id of given img
 * @param $usr_id the id of given user
 * */
function addHate($img_id, $user_id) {
	$query = "select * from imgfavor where iid=$img_id and uid='$user_id' and type=3";
	mysql_query ( $query );
	if (mysql_affected_rows () == 0) {
		$query = "insert into imgfavor(iid,uid,type) values($img_id,'$user_id',3)";
		@mysql_query ( $query );
		$query = "update imgfile set hate=hate+1 where id=$img_id";
		@mysql_query ( $query );
	}
}

function getHate($img_id) {
	$query = "select hate from imgfile where id=$img_id";
	$result = mysql_fetch_array ( mysql_query ( $query ) );
	return $result [0];
}

/*
 * normal a img by a user
 * @param $img_id the id of the given img
 * @param $user_id ths id of the given user
 * */
function addNormal($img_id, $user_id) {
	$query = "select * from imgfavor where iid=$img_id and uid='$user_id' and type=2";
	mysql_query ( $query );
	if (mysql_affected_rows () == 0) {
		$query = "insert into imgfavor(iid,uid,type) values($img_id,'$user_id',2)";
		@mysql_query ( $query );
		$query = "update imgfile set normal=normal+1 where id=$img_id";
		@mysql_query ( $query );
	}
}

function getNormal($img_id) {
	$query = "select normal from imgfile where iid=$img_id";
	$result = mysql_fetch_array ( mysql_query ( $query ) );
	return $result [0];
}

/*
 * add a comment for a img by a user
 * @param $img_id  the id of the given img
 * @param $user_id the id of the given user
 * @param $comment comment by the user
 * */
function addCommet($img_id, $user_id, $comment) {
	$time=date("Y-m-d H:i:s");
	$query="insert into imgcomment(iid,uid,comment,comtime) values($img_id,'$user_id','$comment','$time')";
	mysql_query($query);
	
}

/*
 * get all comments given a img
 * @param $img_id the id of the given img
 * */
function getComment($img_id) {

}

/*
 * add tag(s) to the given img by the user
 * @param $img_id id of the given img
 * @param $user_id id of the given user
 * @param $tag_array array of the tag seperate by %
 * when in database,it's stored as **%**%**%**
 * */
function addTag($img_id, $user_id, $tag_array) {
	$tagStr = "";
	foreach ( $tag_array as $tag_obj ) {
		$tagStr += $tag_obj + "%";
	}
	$tag = substr ( $tagStr, 0, strlen ( $tagStr ) - 1 );
	
	$query = "select * from imgtag where iid='$img_id' and uid='$user_id'";
	$result = mysql_query ( $query );
	if (mysql_affected_rows () == 0) {
		//no previous record then create a record
		$query = "insert into imgtag (iid,uid,tag) values('$img_id','$user_id','$tag')";
		mysql_query ( $query );
	} else {
		//have previous data then update original data
		$obj = mysql_fetch_object ( $result );
		$pre_tag = $obj->tag;
		$tag = $pre_tag . "%" . $tag;
		$query = "update imgtag where ";
		mysql_query ( $query );
	}
}

/*
 * get all tags of a given img
 * @param $img_id id of the img
 * */
function getTag($img_id) {
	$query = "select tagid,tag from imgtag where iid=$img_id";
	$result = mysql_query ( $query );
	$tagStr = "";
	while ( $obj = mysql_fetch_object ( $result ) ) {
		$tagStr = $tagStr . "$obj->tag";
	}
}

//初始化图片，只有16张
function getItemIni() {
	$pageSize = 12;
	$query = "select id,path,imgtitle,favor,hate,normal,
				   count(imgcomment.comment) as com_num,count(imgforward.forward) as for_num 
			from imgfile,imgcomment,imgforward
			where imgcomment.iid=imgfile.id and imgfile.id=imgforward.iid
			order by favor desc
			limit 0,16";
	$result = @mysql_query ( $query );
	$imginfo_array = array ();
	$taginfo_array = array ();
	$taginfo_subarray = array ();
	$likeinfo_array = array ();
	$snsinfo_array = array ();
	$sum_array = array ();
	$final_array = array ();
	while ( $obj = mysql_fetch_object ( $result ) ) {
		$imginfo_array ["img_id"] = $obj->id;
		$imginfo_array ["img_url"] = $obj->path;
		$imginfo_array ["img_title"] = $obj->imgtitle;
		$sum_array ["img_info"] = $imginfo_array;
		
		$img_id = $obj->id;
		$query = "select tagid,tag
				from imgtag
				where iid=$img_id";
		$tag_result = @mysql_query ( $query );
		while ( $tagobj = mysql_fetch_object ( $tag_result ) ) {
			$taginfo_subarray ["id"] = $tagobj->tagid;
			$taginfo_subarray ["name"] = $tagobj->tag;
			$taginfo_array [] = $taginfo_subarray;
		}
		$sum_array ["tag_info"] = $taginfo_array;
		
		$likeinfo_array ["favor"] = $obj->favor;
		$likeinfo_array ["normal"] = $obj->normal;
		$likeinfo_array ["hate"] = $obj->hate;
		$sum_array ["like_info"] = $likeinfo_array;
		
		$snsinfo_array ["comment_num"] = $obj->com_num;
		$snsinfo_array ["forward_num"] = $obj->for_num;
		$snsinfo_array ["share_num"] = 0;
		
		$sum_array ["sns_info"] = $snsinfo_array;
		
		$final_array [] = $sum_array;
	}
	
	return json_encode ( $final_array );
}

/**
 * 每次请求获取对应的图片
 * */
function getItems($col_num, $page_num) {
	$pic_pos = 8 * $page_num + 8 + $col_num;
	$pic_pos_array = array ($pic_pos, $pic_pos + 4 );
	
	foreach ( $pic_pos_array as $pos ) {
		$query = "select id,path,imgtitle,favor,hate,normal,
				count(imgcomment.comment) as com_num,count(imgforward.forward) as for_num
			from imgfile,imgcomment,imgforward
			order by favor desc
			limit $pic_pos,1";		
		$result = mysql_query ( $query );
		$imginfo_array = array ();
		$taginfo_array = array ();
		$likeinfo_array = array ();
		$snsinfo_array = array ();
		$sum_array = array ();
		$final_array = array ();
		
		$obj = mysql_fetch_object ( $result );
		$imginfo_array ["img_id"] = $obj->id;
		$imginfo_array ["img_url"] = $obj->path;
		$imginfo_array ["img_title"] = $obj->imgtitle;
		
		$sum_array ["img_info"] = $imginfo_array;
		
		$img_id = $obj->id;
		$query = "select tagid,tag
				from imgtag
				where iid=$img_id";
		$tag_result = @mysql_query ( $query );
		while ( $tagobj = mysql_fetch_object ( $tag_result ) ) {
			$taginfo_subarray ["id"] = $tagobj->tagid;
			$taginfo_subarray ["name"] = $tagobj->tag;
			$taginfo_array [] = $taginfo_subarray;
		}
		
		$sum_array ["tag_info"] = $taginfo_array;
		
		$likeinfo_array ["favor"] = $obj->favor;
		$likeinfo_array ["normal"] = $obj->normal;
		$likeinfo_array ["hate"] = $obj->hate;
		
		$sum_array ["like_info"] = $likeinfo_array;
		
		$snsinfo_array ["comment_num"] = $obj->com_num;
		$snsinfo_array ["forward_num"] = $obj->for_num;
		$snsinfo_array ["share_num"] = 0;
		
		$sum_array ["sns_info"] = $snsinfo_array;
		
		$final_array [] = $sum_array;
	}
	
	$json_data = json_encode ( $final_array );
	return $json_data;
}

?>